package com.hdl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.hdl.dao.DetailDao;
import com.hdl.dao.MallProductDao;
import com.hdl.entity.MallProduct;
import com.hdl.util.Druidutils;
import com.hdl.util.JdbcUtil;
import com.hdl.vo.DetailVO;

public class DetailDaoImpl implements DetailDao{

	private JdbcTemplate template = new JdbcTemplate(Druidutils.getDataSource());
	private Connection conn = null;
	private PreparedStatement pstm = null;
	private ResultSet rs = null;
	private MallProductDao dao = new MallProductDaolmpl();
	
	@Override
	public List<DetailVO> findAllDetails(String userId, String mallOrderId) {
		List<DetailVO> list = new ArrayList(); 
		//定义sql语句
		String sql = "select a.mall_product_id,a.mall_order_detail_count,a.mall_order_detail_price from mall_order b inner join  mall_order_detail a where a.MALL_ORDER_ID = b.MALL_ORDER_ID AND b.HDL_USER_ID = ? and a.MALL_ORDER_ID = ? ;";
		try {
			conn = JdbcUtil.getConnection();
			//获取预编译对象
			pstm = conn.prepareStatement(sql);
			//赋值
			pstm.setObject(1, userId);
			pstm.setObject(2, mallOrderId);
			//执行
			rs = pstm.executeQuery();
			while(rs.next()) {
				DetailVO vo = new DetailVO();
				String productId = rs.getNString("mall_product_id");
				MallProduct product = dao.SelectProductById(productId);
				vo.setProduct(product);
				vo.setDetailNum(rs.getInt("mall_order_detail_count"));
				vo.setDetailTotal(rs.getInt("mall_order_detail_price"));
				list.add(vo);
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally {
			//释放资源
			JdbcUtil.close(rs,pstm,conn);
		}
		return list;
	}

}
